<?php

namespace app\common\logic;



use app\common\model\shop\Address;
use app\common\model\shop\Order;
use app\common\validate\shop\AddressValidate;
use fast\Http;
use think\Db;
use think\exception\ValidateException;

/**
 * Token接口
 */
class AddressLogic
{
    //地址模型
    protected $addressModel = null;
    protected $key = '';
    private $url = 'https://apis.map.qq.com/ws/geocoder/v1/';
    public function __construct()
    {
        $this->addressModel = new Address();

    }

    public function areaChange() {
        $param = request()->post();
        $province = $param['province'];
        $city = $param['city'];
        $area = $param['area'];
        if ($province) {
            $province = Db::name('area')->where(['level'=>1, 'extra'=> $province])->value('id');
            request()->post(['province' => $province]);
        }
        if ($city) {
            $city = Db::name('area')->where(['level'=>2, 'extra'=> $city])->value('id');
            request()->post(['city' => $city]);

        }
        if ($area) {
            $area = Db::name('area')->where(['level'=>3, 'extra'=> $area])->value('id');
            request()->post(['area' => $area]);
        }

    }
    /**
     * 添加修改
     */
    public function add() {
        $param = request()->post();
        $param['uid'] = UID;
        (new AddressValidate())->goCheck(null, $param);
        Db::startTrans();
        try {
            if ($param['is_default'] == 1) {
                $count = $this->addressModel->where(['uid'=> UID, 'is_default'=> 1])->count();
                if ($count >= 1) {
                    //把其他的地址 为0
                    $this->addressModel->where(['uid'=> UID])->setField('is_default', 0);
                }
            }

            $id = input('id/d', 0);
            $this->addressModel->allowField(true);

            if ($id) {
                $this->addressModel->save($param, ['id'=>$id, 'uid'=> UID]);
            } else {
                $this->addressModel->data($param)->save();
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            throw new ValidateException($e->getMessage());
        }

        return true;
    }

    /**删除地址*/
    public function del($id = 0) {
        $where['uid'] = UID;
        $where['id'] = $id;
        $res = $this->addressModel->where($where)->delete();

        if (!$res) {
            throw new ValidateException('删除失败');
        }

        return true;
    }

    /**地址列表*/
    public function addressList() {
        $list = $this->addressModel->order('createtime desc')->where('uid', UID)->paginate();

        return $list;
    }

    /**
     * 获取默认收获地址
     */
    public function getDefault() {
        $where['uid'] = UID;
        $where['is_default'] = 1;
        $info = $this->addressModel->where($where)->find();

        return $info;
    }
    public function detail($id) {
        $where['uid'] = UID;
        $where['id'] = $id;
        $info = $this->addressModel->where($where)->find();

        return $info;
    }

    //设置订单收获地址
    public function setAddress() {
        $orderid = input('orderid/d', 0);
        $address_id = input('address_id/d', 0);
        $address = $this->addressModel->where(['id'=> $address_id])->find();

        if (!$address_id || !$address) {
            throw new ValidateException('请传递地址id');
        }

        $data['province'] = $address['province'];
        $data['city'] = $address['city'];
        $data['area'] = $address['area'];
        $data['detail'] = $address['detail'];
        $data['address_id'] = $address['address_id'];
        (new Order())->where(['id'=> $orderid])->update($data);

        return true;
    }

}
